<%-- 
    Document   : receipt
    Created on : Mar 16, 2012, 4:50:57 PM
    Author     : 013v
--%>

<%@page import="Model.Transaction"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="Model.CustomerManager"%>
<%@page import="Model.ProductManager"%>

<%

    String loginAttempt = request.getParameter("loginAttempt");
    if (loginAttempt != null) {
        boolean isValid = true;

        if (loginAttempt.indexOf("<") > -1) {
            isValid = false;
        } else if (loginAttempt.indexOf(">") > -1) {
            isValid = false;
        } else if (loginAttempt.indexOf("\"") > -1) {
            isValid = false;
        } else if (loginAttempt.indexOf("/") > -1) {
            isValid = false;
        } else if (loginAttempt.indexOf(":") > -1) {
            isValid = false;
        } else if (loginAttempt.indexOf(";") > -1) {
            isValid = false;
        } else if (loginAttempt.indexOf("=") > -1) {
            isValid = false;
        } else if (loginAttempt.indexOf("|") > -1) {
            isValid = false;
        }

        if (isValid == false) {
            loginAttempt = null;
        }
          }     
     String loginAttemptMax = request.getParameter("loginMax");
    if (loginAttempt != null) {
        boolean isValidMax = true;

        if (loginAttemptMax.indexOf("<") > -1) {
            isValidMax = false;
        } else if (loginAttemptMax.indexOf(">") > -1) {
            isValidMax = false;
        } else if (loginAttemptMax.indexOf("\"") > -1) {
            isValidMax = false;
        } else if (loginAttemptMax.indexOf("/") > -1) {
            isValidMax = false;
        } else if (loginAttemptMax.indexOf(":") > -1) {
            isValidMax = false;
        } else if (loginAttemptMax.indexOf(";") > -1) {
            isValidMax = false;
        } else if (loginAttemptMax.indexOf("=") > -1) {
            isValidMax = false;
        } else if (loginAttemptMax.indexOf("|") > -1) {
            isValidMax = false;
        }

        if (isValidMax == false) {
            loginAttemptMax = null;
        }
    }
%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" href="css/foo.css" type="text/css"/>
        <link rel="stylesheet" type="text/css" href="css/superfish.css" media="screen">	
        <title>Foobar</title>
        <script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
        <script type="text/javascript" src="js/hoverIntent.js"></script>
        <script type="text/javascript" src="js/superfish.js"></script>
        <script type="text/javascript" src="js/LoginValidation.js"></script>
        <script type="text/javascript" src="js/SearchValidation.js"></script>
        <script type="text/javascript">

            // initialise plugins
            jQuery(function(){
                jQuery('ul.sf-menu').superfish();
            });
            
            function checkAuth(){
              if(<%=request.getParameter("loginAttempt")%>==false && isValid){
                 document.write('<span class="errLog">Authentication failed.</span>');
               }
               else if (<%=request.getParameter("loginMax")%>==true && isValidMax){
                   document.write('<span class="errLog">You have reached your maximum login attempts. Please try again later.</span>');
               }
            }
           

        </script>
    </head>
    <body class="bg">
        <br/>
        <div class="hmenu">
            <% if (request.getSession(true).getAttribute("username") == null) {%>
            <p style="font-size: 12px; margin-top: -15px; float: right;">If you're not yet a member, sign up <a href="signUp.jsp" class="lin">here!</a> </p>   
            <% }%>
            <img src="images/head.png" style="float: left; margin-left:20px;"/>
            <div class="log">
                <% if (request.getSession(true).getAttribute("username") != null) {%>
                <span>Hello <%=session.getAttribute("username")%>!</span><br>
                <span style="font-weight: normal; font-size: 10px;">You have <%=session.getAttribute("numCart")%> items in your cart.</span>
                <div id="nav">
                    <ul class="top">
                        <li><a href="viewCart.jsp">View Cart</a></li>
                        <li><a href="checkOut.jsp">Checkout</a></li>
                        <li><a href="viewProfile.jsp">Account Details</a></li>
                        <li><a href="LogoutServlet">Log out</a></li>
                    </ul>
                </div>
                <% } else {%>
                <span style="font-weight: bold;">Log in</span>
                <script type="text/javascript">checkAuth();</script>
                <form method="post" action="LoginServlet">
                    <label>Username:</label><input type="text" id="username" name="username"/><br/>
                    <label>Password:</label><input type="password" id="password" name="password"/><br/>
                    <input type="submit" onClick="return LoginValidation()" value="Log in" name="submit"/>
                    <label class="errLog" id="err"></label>
                </form>
                <% }%>
            </div>
            <ul class="sf-menu">
                <li class="current">
                    <a href="index.jsp">Home</a>
                </li>
                <li>
                    <a href="#">Products</a>
                    <ul>
                                        <%
                ResultSet rs = new ProductManager().getProductTypes();
                while(rs.next()){
                %>
                        <li>
                            <a href="products.jsp?prd=<%=rs.getInt("ProductTypeID")%>"><%=rs.getString("ProductType")%></a>
                        </li>
                <%}%>
                    </ul>
                </li>
                <li>
                    <a href="#">Stores</a>
                </li>
                <li>
                    <a href="#">Contact Us</a>
                </li>
                <li>
                    <form action="results.jsp">
                            <input type="text" name="search" placeholder="Enter your search"/>
                            <button type="submit" onClick="return SearchValidation(this.form);">Go</button>
                    </form>
                </li>
            </ul>
        </div>
        <div class="content">
            <br/><br/>

            <%
                Transaction newTrans = new CustomerManager().getTrans((String) session.getAttribute("username"));
                ResultSet cd = new CustomerManager().getCredDel((String) session.getAttribute("username"));

                if (cd.next()) {
            %>
            <table>
                <tr><td><label class="data">Purchase Receipt</label></td></tr>
                <tr>
                    <td><label class="data">Receipt no: </label></td>
                    <td><span class="info"><%=newTrans.getTransactionID()%>&nbsp;&nbsp;&nbsp;&nbsp;<%=newTrans.getTransactionTimeStamp()%></span></td>
                </tr>
                <tr>
                    <td><label class="data">Credit Card: </label></td>
                    <td><span class="info"><%=cd.getString("CredNum")%></span></td>
                </tr>
                <tr>
                    <td><label class="data">Delivery Address: </label></td>
                    <td><span class="info"><%=cd.getString("DeliveryAdd")%></span></td>
                </tr>
            </table>
            <% }
                cd.close();

                ResultSet pit = new CustomerManager().getPurchaseItems(Integer.toString(newTrans.getTransactionID()));
                
            %>
            <table style="margin-left: auto; margin-right: auto; border-spacing: 3px;">
                <tr style="background-color: #BDD2FF; color: #cc5a5a;">
                    <th width="400">Product</th>
                    <th width="50">Quantity</th>
                    <th width="100">Price</th>
                </tr>
                <% while (pit.next()) {%>
                <tr>
                    <td align="center"> <%=pit.getString("ProdTitle")%> </td>
                    <td align="center"> <%=pit.getString("Quantity")%> </td>
                    <td align="right">
                        Php <%=pit.getString("Price")%>
                    </td>
                    <% }
                        pit.close();

                    %>    
                </tr>
                <tr>
                    <td colspan="2" align="right">Total:</td>
                    <td><%=newTrans.getTransactionTotal()%></td>
                </tr>

                <tr>
                    <td colspan="4" align="right"><a href="index.jsp"><button>Finish</button></a></td>
                </tr>
            </table>
        </div>
    </body>
</html>
